package com.cuckoo.cuckoo_app2.ui;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.apiAsync.BaseCallbac;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.SiteLinkApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.SiteLinkApi;
import com.cuckoo.cuckoo_app2.dao.SubmitPlatform;
import com.cuckoo.cuckoo_app2.utils.DialogUtils;
import com.google.gson.JsonObject;

public class AddPlatformActivity extends AppCompatActivity {

    private Context context;
    private ViewHolder holder;
    private String radioValue = "add";
    private SiteLinkApi siteLinkApi;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Log.i("TEST", "AddPlatformActivity.onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_platform);

        this.context = this;
        this.holder = new ViewHolder();
        this.siteLinkApi = new SiteLinkApiImp();

        holder.radioAdd.setChecked(true);

        // 绑定按钮事件
        holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.d("TEST", "checkedId: " + checkedId);
                if (checkedId == R.id.radioAdd) {
                    radioValue = "add";
                } else if (checkedId == R.id.radioUpdate) {
                    radioValue = "updata";
                }
            }
        });

        holder.submit.setOnClickListener(v -> {
            submit();
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 提交
     */
    public void submit() {
        Log.d("TEST", "submit");
        SubmitPlatform submitPlatform = new SubmitPlatform();
        // 类型
        submitPlatform.setType(radioValue);
        if (radioValue.isEmpty()) {
            DialogUtils.showToast(context, "请输入平台类型");
            return;
        }
        // 平台名称
        submitPlatform.setName(holder.platformName.getText().toString());
        if (submitPlatform.getName().isEmpty()) {
            DialogUtils.showToast(context, "请输入平台名称");
            return;
        }
        // app下载地址
        submitPlatform.setAppAddress(holder.appAddress.getText().toString());
        if (submitPlatform.getAppAddress().isEmpty()) {
            DialogUtils.showToast(context, "请输入app下载地址");
            return;
        }
        // 网页登录地址
        submitPlatform.setWebAddress(holder.webAddress.getText().toString());
        if (submitPlatform.getWebAddress().isEmpty()) {
            DialogUtils.showToast(context, "请输入网页登录地址");
            return;
        }
        // 平台账号
        submitPlatform.setAccount(holder.account.getText().toString());
        // 平台密码
        submitPlatform.setPassword(holder.password.getText().toString());
        // 提交

        siteLinkApi.SiteLinkAddAsync(submitPlatform, new BaseCallbac<JsonObject>() {

            @Override
            public void onSuccess(JsonObject data) {
                runOnUiThread(() -> {
                    if (!data.get("success").getAsBoolean()) {
                        DialogUtils.showToast(context, data.get("message").getAsString());
                        return;
                    }
                    DialogUtils.showToast(context, "提交成功");
                    // 暂停一秒
                    DialogUtils.sleep(1000);
                    // 返回上一页
                    finish();
                });
            }

            @Override
            public void onFailure(String errorMessage) {
                runOnUiThread(() -> {
                    DialogUtils.showToast(context, errorMessage);
                });
            }
        });
    }

    class ViewHolder {
        RadioGroup radioGroup;
        RadioButton radioAdd;
        RadioButton radioUpdate;
        EditText platformName;
        EditText appAddress;
        EditText webAddress;
        EditText account;
        EditText password;
        Button submit;

        public ViewHolder() {
            radioGroup = findViewById(R.id.radioGroup);
            radioAdd = findViewById(R.id.radioAdd);
            radioUpdate = findViewById(R.id.radioUpdate);
            platformName = findViewById(R.id.platformName);
            appAddress = findViewById(R.id.appAddress);
            webAddress = findViewById(R.id.webAddress);
            account = findViewById(R.id.account);
            password = findViewById(R.id.password);
            submit = findViewById(R.id.submit);

        }
    }


}
